<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class website_featured_media extends CI_Controller {
	public function __construct() {
		session_start ();
		parent::__construct ();
		$this->load->helper ( "common" );
	}
	public function index() {
		$this->featured_media_main ();
	}
	public function featured_media_main() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		
		$data ['edit_featured_media'] = false;
		if (! $this->right_manage->check_right ( "edit_featured_media" )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		$data['media_type'] = $this->input->get ( "media_type" );
		if ($data['media_type'] == "") {
			$data['media_type'] = "common";
		}
		
		$this->load->library ( "website/featured_media" );
		$data ['featured_media_list'] = $this->featured_media->query_featured_media_list ( $data['media_type'] );
		$this->load->view ( "website/featured_media/featured_media_main", $data );
	}
	public function add_featured_media() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		$data ['edit_featured_media'] = false;
		if (! $this->right_manage->check_right ( "edit_featured_media" )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		$data ['media_type'] = $this->input->get ( "media_type" );
		if ($data ['media_type'] == "") {
			$data ['media_type'] = "common";
		}
		
		$this->load->library ( "website/featured_media" );
		$data ['sort_num'] = $this->featured_media->query_sort_num ();
		$this->load->view ( "website/featured_media/add_featured_media", $data );
	}
	public function save_featured_media() {
		$this->load->library ( "common/login_manage" );
		if ($this->login_manage->check_login () == false) {
			$data ['result'] = false;
			$data ['message'] = '请您先登录';
			echo json_encode ( $data );
			die ();
		}
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "edit_featured_media", $_SESSION ['user_id'] )) {
			$result ['result'] = false;
			$result ['message'] = '你没有权限使用这个功能';
			echo json_encode ( $result );
			die ();
		}
		$featured_media ['media_id'] = trim ( $this->input->get_post ( "media_id" ) );
		$featured_media ['sort_num'] = intval ( $this->input->get_post ( "sort_num" ) );
		$featured_media ['media_type'] = $this->input->get_post ( "media_type" );
		if ($featured_media ['media_type'] == "") {
			$featured_media ['media_type'] = "common";
		}
		$this->load->library ( "website/featured_media" );
		$this->load->helper ( "common" );
		$result = $this->featured_media->save_featured_media ( $featured_media );
		echo JSON ( $result );
	}
	public function edit_featured_media() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		$data ['edit_featured_media'] = false;
		if (! $this->right_manage->check_right ( "edit_featured_media" )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		$featured_id = $this->input->get ( "featured_id" );
		$this->load->library ( "website/featured_media" );
		$data ['featured_media_info'] = $this->featured_media->query_featured_media_info ( $featured_id );
		$this->load->view ( "website/featured_media/edit_featured_media", $data );
	}
	public function update_featured_media() {
		$this->load->library ( "common/login_manage" );
		if ($this->login_manage->check_login () == false) {
			$data ['result'] = false;
			$data ['message'] = '请您先登录';
			echo json_encode ( $data );
			die ();
		}
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "edit_featured_media", $_SESSION ['user_id'] )) {
			$result ['result'] = false;
			$result ['message'] = '你没有权限使用这个功能';
			echo json_encode ( $result );
			die ();
		}
		$featured_id = $this->input->get_post ( "featured_id" );
		$featured_media ['media_id'] = trim ( $this->input->get_post ( "media_id" ) );
		$featured_media ['sort_num'] = intval ( $this->input->get_post ( "sort_num" ) );
		$featured_media ['media_type'] = $this->input->get_post ( "media_type" );
		$this->load->library ( "website/featured_media" );
		$this->load->helper ( "common" );
		$result = $this->featured_media->update_featured_media ( $featured_media, $featured_id );
		echo JSON ( $result );
	}
	public function delete_featured_media() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		$data ['edit_featured_media'] = false;
		if (! $this->right_manage->check_right ( "edit_featured_media" )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		$featured_id = $this->input->get ( "featured_id" );
		$this->load->library ( "website/featured_media" );
		$this->featured_media->delete_featured_media ( $featured_id );
		echo "<script>history.go(-1)</script>";
	}
}